﻿
using OnlineMealOrdering.Common;
using OnlineMealOrdering.Domain.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineMealOrdering.Domain
{
    public class RoleEntity : AuditableEntity
    {
        public string RoleCode { get; set; }

        public string RoleName { get; set; }

        public virtual ICollection<UserEntity> Users { get; set; }

        public void AddUser(UserEntity user)
        {
            if (user == null)
                throw new BusinessException("往角色中添加的用户为空。");

            if (Users == null)
                Users = new List<UserEntity>();

            Users.Add(user);
        }

        public static RoleEntity CreateRole(string roleCode, string roleName)
        {
            var reduplicateRole = DomainContext.Current.EntitySet<RoleEntity>().SingleOrDefault(a => a.RoleCode == roleCode || a.RoleName == roleName);
            if (reduplicateRole != null)
                throw BusinessException.Create("角色{0}已存在",roleName);

            RoleEntity role = new RoleEntity()
            {
                RoleCode = roleCode,
                RoleName = roleName
            };
            return role;
        }
    }
}
